package kubernetes

import (
	"k8s-admin/service/kubernetes"
	"k8s-admin/utils"
	"time"

	"github.com/gin-gonic/gin"
)

func CreateClusterDaemonSet(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `json:"namespace" binding:"required"`
		DaemonSetData string `json:"daemonSet_data" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBindJSON(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		err = kubernetes.NewK8sInterface().CreteClusterDaemonSet(cid, params.Namespace, params.DaemonSetData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "创建成功")
		}
	}

}

func GetClusterDaemonList(ctx *gin.Context) {
	params := new(struct {
		Namespace string `form:"namespace" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		daemonSetList, err := kubernetes.NewK8sInterface().GetClusterDaemonSetList(cid, params.Namespace)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
			return
		}
		list := []map[string]interface{}{}

		for _, item := range daemonSetList.Items {
			list = append(list, map[string]interface{}{
				"name":          item.Name,
				"Image":         item.Spec.Template.Spec.Containers[0].Image,
				"namespace":     item.Namespace,
				"labels":        item.Labels,
				"node_selector": item.Spec.Selector.MatchLabels,
				"creationTimestamp": item.CreationTimestamp.Time.Format(time.DateTime),
			})
		}
		utils.ReturnContext(ctx).Successful("成功", list)

	}

}

func GetClusterDaemonSetDetail(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `form:"namespace" binding:"required"`
		DaemonSetName string `form:"daemonSet_name" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		daemonSetDetail, err := kubernetes.NewK8sInterface().GetClusterDaemonSetInfo(cid, params.Namespace, params.DaemonSetName)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
			return
		}
		utils.ReturnContext(ctx).Successful("成功", daemonSetDetail)

	}

}

func DeleteClusterDaemonSet(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `form:"namespace" binding:"required"`
		DaemonSetName string `form:"daemonSet_name" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		err = kubernetes.NewK8sInterface().DeleteClusterDaemonSet(cid, params.Namespace, params.DaemonSetName)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "删除成功")
		}
	}

}

func UpdateClusterDaemonSet(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `json:"namespace" binding:"required"`
		DaemonSetData string `json:"daemonSet_data" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBindJSON(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		err = kubernetes.NewK8sInterface().UpdateClusterDaemonSet(cid, params.Namespace, params.DaemonSetData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "更新成功")
		}
	}

}
